﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using GalaSoft.MvvmLight.Messaging;
using HandyControl.Controls;
using Infov1.Commands;
using Infov1.Models;
using SqlSugar;

namespace Infov1.ViewModels.WorkRecord
{
    /// <summary>
    /// 客户选择对话框的ViewModel
    /// </summary>
    public class CustomerSelectViewModel : ViewModelBase
    {
        #region 私有字段
        private readonly ISqlSugarClient _db;
        private ObservableCollection<CustomerInfoModel> _customerList;
        private CustomerInfoModel _selectedCustomer;
        private string _search;
        #endregion

        #region 构造函数
        /// <summary>
        /// 初始化客户选择ViewModel
        /// </summary>
        /// <param name="initialSearchText">初始搜索文本</param>
        public CustomerSelectViewModel(string initialSearchText = "")
        {
            _db = DbContext.GetConnection();
            SearchCommand = new RelayCommand(SearchInfo);

            // 设置初始搜索文本并执行搜索
            Search = initialSearchText;
            if (!string.IsNullOrEmpty(initialSearchText))
            {
                SearchInfo();
            }
            else
            {
                LoadCustomers();
            }
        }
        #endregion

        #region 公共属性
        /// <summary>
        /// 客户列表集合
        /// </summary>
        public ObservableCollection<CustomerInfoModel> CustomerList
        {
            get => _customerList;
            set
            {
                _customerList = value;
                OnPropertyChanged(nameof(CustomerList));
            }
        }

        /// <summary>
        /// 搜索文本
        /// </summary>
        public string Search
        {
            get => _search;
            set
            {
                _search = value;
                OnPropertyChanged(nameof(Search));
            }
        }

        /// <summary>
        /// 选中的客户
        /// </summary>
        public CustomerInfoModel SelectedCustomer
        {
            get => _selectedCustomer;
            set
            {
                _selectedCustomer = value;
                OnPropertyChanged(nameof(SelectedCustomer));
            }
        }

        /// <summary>
        /// 对话框结果
        /// </summary>
        public bool? DialogResult { get; set; }
        #endregion

        #region 命令
        /// <summary>
        /// 搜索命令
        /// </summary>
        public ICommand SearchCommand { get; }
        #endregion

        #region 私有方法
        /// <summary>
        /// 搜索客户信息
        /// </summary>
        private void SearchInfo()
        {
            
            try
            {
                var query = _db.Queryable<CustomerInfoModel>()
                    .Where(x => x.CustomerCode.Contains(Search) ||
                               x.CustomerCodeTd.Contains(Search) ||
                                x.ContactPerson.Contains(Search) ||
                               x.CompanyName.Contains(Search))
                    .Select(x => new CustomerInfoModel
                    {
                        CustomerCode = x.CustomerCode,
                        CustomerCodeTd = x.CustomerCodeTd,
                        CompanyName = x.CompanyName,
                        ContactPerson = x.ContactPerson,
                        ContactPhone = x.ContactPhone,
                        Address = x.Address,
                        Id=x.Id
                    })
                    .OrderBy(x => x.CustomerCode);

                var customers = query.ToList();
                CustomerList = new ObservableCollection<CustomerInfoModel>(customers);
            }
            catch (Exception ex)
            {
                Growl.Error($"搜索客户失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 加载所有客户列表
        /// </summary>
        private void LoadCustomers()
        {
            try
            {
                if (_db == null) return;

                var customers = _db.Queryable<CustomerInfoModel>()
                    .Select(x => new CustomerInfoModel
                    {
                        CustomerCode = x.CustomerCode,
                        CustomerCodeTd = x.CustomerCodeTd,
                        CompanyName = x.CompanyName,
                        ContactPerson = x.ContactPerson,
                        ContactPhone = x.ContactPhone,
                        Address = x.Address
                    })
                    .OrderBy(x => x.CustomerCode)
                    .ToList();

                CustomerList = new ObservableCollection<CustomerInfoModel>(customers);
            }
            catch (Exception ex)
            {
                Growl.Error($"加载数据失败：{ex.Message}");
            }
        }
        #endregion
    }
}